package cn.Robbery.org.controller;

import cn.Robbery.basic.util.AjaxResult;
import cn.Robbery.basic.util.PageList;
import cn.Robbery.org.domain.Department;
import cn.Robbery.org.query.DepartmentQuery;
import cn.Robbery.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.ws.Service;
import java.util.List;

@RestController
@RequestMapping("/department")
@Api(tags = "部门接口",description = "部门接口详细描述")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    @PutMapping
    @ApiOperation(value = "部门添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if(department.getId() == null){
                departmentService.add(department);
            }else{
                departmentService.update(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙，稍后重试!"+ e.getMessage());
        }
    }


    @GetMapping("/{id}")  //http://localhost:8080/department/1
    @ApiOperation(value = "查询一条部门数据",notes = "需要传入id")
    public Department getById(@PathVariable("id") Long id){
        return departmentService.getById(id);
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setMessage("系统繁忙，稍后重试!"+e.getMessage());
        }

    }

    @PostMapping
    public PageList<Department> queryPage(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.queryPage(departmentQuery);
    }

    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try{
            departmentService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除失败!"+e.getMessage());
        }
    }

    @GetMapping("/tree")
    public List<Department> getDeoartment(){
        return departmentService.getDepTree();
    }
}
